/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.global.webservice;


import com.timesoft.webservice.client.base.BaseWebService;
import com.timesoft.webservice.client.base.BaseWs;
import com.timesoft.webservice.client.setting.SettingWebService;
import com.timesoft.webservice.client.setting.SettingWs;
import java.net.URL;
import org.apache.log4j.Logger;

/**
 *
 * @author sorasaks
 */
public class WebServiceClient {

    private static final Logger LOG = Logger.getLogger(WebServiceClient.class);
    private static WebServiceClient instance;
    
    private static BaseWebService baseService = null;
    private static SettingWebService settingService = null;

    public static WebServiceClient getInstance() {
        if (instance == null) {
            instance = new WebServiceClient();
        }
        return instance;
    }
    
    public static BaseWebService getBaseService() {
        return getBaseService(null);
    }

    public static BaseWebService getBaseService(URL url) {
        try {
            if(baseService == null)
            {
                synchronized(WebServiceClient.class)
                {
                    if(baseService == null)
                    {
                        if (url != null) {
                            baseService = (BaseWebService) (new BaseWs(url)).getBaseWsPort();
                        } else {
                            baseService = (BaseWebService) (new BaseWs()).getBaseWsPort();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOG.error("Load CommonWebService Error URL : " + url);
            LOG.error(e.getMessage(), e);
        }

        return baseService;
    }
    
    public static SettingWebService getSettingService() {
        return getSettingService(null);
    }

    public static SettingWebService getSettingService(URL url) {
        try {
            if(settingService == null)
            {
                synchronized(WebServiceClient.class)
                {
                    if(settingService == null)
                    {
                        if (url != null) {
                            settingService = (SettingWebService) (new SettingWs(url)).getSettingWsPort();
                        } else {
                            settingService = (SettingWebService) (new SettingWs()).getSettingWsPort();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOG.error("Load CommonWebService Error URL : " + url);
            LOG.error(e.getMessage(), e);
        }

        return settingService;
    }
}
